package com.taokatao.cardmee.business;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taokatao.cardmee.App;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.business.login.LoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class PrimaryMenuLayout extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.i_card) ImageView iCard;
    @BindView(R.id.t_card) TextView tCard;
    @BindView(R.id.i_more) ImageView iMore;
    @BindView(R.id.t_more) TextView tMore;
    @BindView(R.id.i_profile) ImageView iProfile;
    @BindView(R.id.t_profile) TextView tProfile;

    private Callback callback;

    private byte currMenu = Menu.None;

    public PrimaryMenuLayout(Context context) {
        this(context, null);
    }

    public PrimaryMenuLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(LayoutInflater.from(context).inflate(R.layout.layout_primary_menu, this));
    }

    public void setCallback(Callback cb) {
        callback = cb;
    }

    public void clickMenu(byte clickedMenu) {
        if (clickedMenu == currMenu) return;
        switch (clickedMenu) {
            case Menu.Card:
                iCard.setImageResource(R.mipmap.main_card_selected);
                tCard.setTextColor(0xFFFF356F);
                iMore.setImageResource(R.mipmap.main_more_normal);
                tMore.setTextColor(0xFF888888);
                iProfile.setImageResource(R.mipmap.main_profile_normal);
                tProfile.setTextColor(0xFF888888);
                Utils.onMobEvent(getContext(), "clickCardPackageOfNavigation");
                break;
            case Menu.More:
                iCard.setImageResource(R.mipmap.main_card_normal);
                tCard.setTextColor(0xFF888888);
                iMore.setImageResource(R.mipmap.main_more_selected);
                tMore.setTextColor(0xFFFF356F);
                iProfile.setImageResource(R.mipmap.main_profile_normal);
                tProfile.setTextColor(0xFF888888);
                Utils.onMobEvent(getContext(), "clickFindOfNavigation");
                break;
            case Menu.Profile:
                iCard.setImageResource(R.mipmap.main_card_normal);
                tCard.setTextColor(0xFF888888);
                iMore.setImageResource(R.mipmap.main_more_normal);
                tMore.setTextColor(0xFF888888);
                iProfile.setImageResource(R.mipmap.main_profile_selected);
                tProfile.setTextColor(0xFFFF356F);
                Utils.onMobEvent(getContext(), "clickMEOfNavigation");
                break;
            default:
                clickedMenu = Menu.None;
                break;
        }

        currMenu = clickedMenu;
        if (callback != null) {
            callback.onPrimaryMenuClick(clickedMenu);
        }
    }

    @Override
    @OnClick({R.id.l_card_menu, R.id.l_more_menu, R.id.l_profile_menu})
    public void onClick(View v) {
        byte clickedMenu = Menu.None;
        switch (v.getId()) {
            case R.id.l_card_menu:
                clickedMenu = Menu.Card;
                break;
            case R.id.l_more_menu:
                clickedMenu = Menu.More;
                break;
            case R.id.l_profile_menu:

                if (App.i().getCurrUser() == null || TextUtils.isEmpty(App.i().getCurrUser().getUserTel())) {
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    getContext().startActivity(intent);
                    return;
                }

                clickedMenu = Menu.Profile;
                break;
        }
        clickMenu(clickedMenu);
    }

    public static class Menu {
        final static byte None = 0;
        final static byte Card = 1;
        final static byte More = 2;
        final static byte Profile = 3;
    }

    public interface Callback {
        void onPrimaryMenuClick(int selected);
    }
}
